/**
 * python gen: Fri Jun 11 10:54:47 2021
 */

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  sysuserService,
  sysdeptService,
  sysroleService
} from "../../../api/apiService";
import sysuserForm from "./sysuserForm.vue";
import sysuserView from "./sysuserView.vue";

export default {
  name: "sysuserHome",
  props: ["deptId"],
  components: {
    sysuserForm,
    sysuserView,
    Treeselect
  },
  data() {
    return {
      roleselect: [],
      deptselect: [],
      options: [
        {
          value: "1",
          label: "通过"
        },
        {
          value: "2",
          label: "不通过"
        },
        {
          value: "0",
          label: "未检查"
        }
      ],
      searchForm: {
        // name: "",
        CHECK_RESULT: "",
        sort: "",
        deptId: ""
      },
      multipleSelection: [],
      tableData: [],
      currentPage: 1,
      totalCount: 0,
      PageSize: this.pageSizes[0],
      dialogFormVisible: false,
      dataFormKey: "-1",
      addUpdateFlag: "add",
      dataItem: {},
      onlyView: false,
      dialogViewVisible: false
    };
  },
  mounted() {
    // do sth
    this.getList(true);
    this.loadDeptTree();
    this.loadRoles();
  },

  methods: {
    //todo

    loadRoles() {
      sysroleService.all().then(res => {
        let { data } = res;
        this.roleselect = data;
      });
    },

    loadDeptTree() {
      sysdeptService.tree().then(res => {
        let { data } = res;
        this.deptselect = data;
      });
    },

    handleSortChange({ prop, order }) {
      // console.log(column, prop, order);
      if (!order) {
        return;
      }
      if ("descending" === order) {
        this.searchForm.sort = `${prop},desc`;
      }
      if ("ascending" === order) {
        this.searchForm.sort = `${prop}`;
      }

      this.getList();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
      this.getList(true);
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
      this.getList();
    },

    resetForm() {
      this.$refs["searchForm"].resetFields();
      this.getList();
    },
    getList(flag) {
      flag && (this.currentPage = 1);
      let params = {
        size: this.PageSize,
        page: this.currentPage - 1,
        ...this.searchForm,
        deptId: this.deptId
      };
      sysuserService.queryData(params).then(res => {
        let {
          data: { total, records }
        } = res;
        this.totalCount = total;
        this.tableData = records;
      });
    },

    doPassReset() {
      this.$prompt("修改密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/,
        inputErrorMessage: "8-20位数字和字母的组合"
      }).then(({ value }) => {
        let item = this.multipleSelection[0];
        let { oid } = item;
        let params = {
          oid,
          upass: this.$md5(value)
        };
        sysuserService.passReset(params);
      });
    },

    // to 详情
    goView(item) {
      this.onlyView = true;
      this.dataItem = item;
      this.addUpdateFlag = "view";
      this.dataFormKey = this.$uuidv1();

      this.dialogViewVisible = true;
    },

    // to 新增
    goAdd() {
      this.onlyView = false;
      this.addUpdateFlag = "add";
      this.dataItem = {};
      this.dataFormKey = this.$uuidv1();
      this.dialogFormVisible = true;
    },

    doValid() {
      this.$refs.dataForm.validateIt();
    },

    // to 编辑
    goUpdate1() {
      let item = this.multipleSelection[0];
      sysuserService.getById({ id: item.oid }).then(res => {
        let { data } = res;
        this.goUpdate(data);
      });
    },
    // to 编辑
    goUpdate(item) {
      this.onlyView = false;
      // let item = this.multipleSelection[0];
      this.dataItem = JSON.parse(JSON.stringify(item));
      this.addUpdateFlag = "update";
      this.dataFormKey = this.$uuidv1();
      this.dialogFormVisible = true;
    },

    // 新增，修改 后， 点击[保存]
    doSave() {
      this.dataItem.password = this.dataItem.password || this.$md5("123456");
      sysuserService.postItem(this.dataItem).then(() => {
        this.dialogFormVisible = false;
        this.getList();
      });
    },

    // to delete
    goDelete() {
      this.$confirm("确定要删除数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.doDelete();
        })
        .catch(() => {});
    },

    doDelete() {
      let ids = this.multipleSelection.map(it => it.oid);
      let params = { ids };
      sysuserService.deleteItem(params).then(() => {
        this.getList();
      });
    },

    // to check
    // doCheck() {
    //   let params = { tables: "SYSUSER" };
    //   apiService.checkItem(params).then((res) => {
    //     let { total, errors } = res;
    //     this.$message({
    //       message: `总扫描记录行数: ${total}, 发现错误数: ${errors} `,
    //       type: "success",
    //     });
    //     this.getList();
    //   });
    // },

    // to import
    goImport() {},
    // to export
    goExport() {
      // let params = {
      //   tableCode: "SYSUSER",
      //   desensitize: true,
      // };
      // // apiService.exportItem(params);
      // let baseUrl = process.env.VUE_APP_BASE_URL;
      // window.open(
      //   `${baseUrl}/collect/packet/export?tableCode=${params.tableCode}&desensitize=${params.desensitize}`
      // );
    }
  }
};
